<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Guestbook.php 134 2009-05-24 09:48:43Z movoin $
 */

/**
 * Controller_Guestbook “客户留言”控制器
 *
 * @package Site
 * @subpackage Front
 *
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_Guestbook extends Controller_Base
{
    var $modelGuestbook;

    /**
     * 构造函数
     *
     * @return Controller_Guestbook
     */
    function Controller_Guestbook()
    {
        // 构造父类
        parent::Controller_Base();
        $this->modelGuestbook =& FLEA::getSingleton( 'Model_Guestbook' );
    }

    /**
     * 显示控制台
     *
     */
    function actionIndex()
    {
        $pagesize = 5;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        //缓存名称
        $cache_id = 'guestbook.list.page' . $page;
        //从缓存中读取数据
        $data = FLEA::getCache($cache_id, $this->lifetime, true, true);
        if(!is_array($data)) {
            $table = $this->modelGuestbook->getTable();
            $where = array(
                array( 'col_key', 'guestbook' ),
                array( 'displayorder', 1 )
                );
            FLEA::loadHelper( 'pager' );
            $pager =& new FLEA_Helper_Pager($table, $page, $pagesize, $where, 'created DESC');
            $data['pager'] = $pager->getPagerData();
            $data['rows'] = $pager->findAll('comment_id,content,options,reply,created');
            FLEA::writeCache( $cache_id, $data, true );
        }
        FLEA::loadFile( 'Helper_Common' );
        $this->_executeView('guestbook-list', $data);
    }

    function actionWrite()
    {
        $this->_executeView('guestbook-write');
    }

    function actionSave()
    {
        if(!$_POST['username'])
            js_alert('姓名必须填写!', 'history.back(-1)');
        if(!$_POST['email'])
            js_alert('联系邮箱必须填写!', 'history.back(-1)');
        if(!$_POST['content'])
            js_alert('留言内容必须填写!', 'history.back(-1)');

        js_alert($this->modelGuestbook->saveMessage('guestbook', $_POST), 0, url('guestbook', 'write'));
    }

}
